/**
 * Created by weidong on 14-2-21.
 */

var log = require("../lib/log");
var cfg = require('../config');
var url = require('url');
var querystring = require('querystring');
var db = require('../lib/db.js');

exports.snauthcontrolservlet = function(request,response)
{
    //解析query
    var retpath ='/portal/auth?';
    //参数decode
    var querystr = decodeURIComponent(url.parse(request.url).query);
    console.log("[SnAuthControl.Servlet]:receive request's query string  = " + querystr);
    /* test code
    console.log("[SnAuthControl.Servlet]:split(&)[1]:"+querystr.split("&")[1]);
    var tmpstr = querystr.split("&")[1];
    console.log("[SnAuthControl.Servlet]:split(&)[1]:"+ tmpstr);
    querystr = tmpstr.substring(6);
    console.log("[SnAuthControl.Servlet]:substring of split(&)[1]:"+tmpstr +" is " + querystr);
     var param = querystr.split("-");
    */
    var param = querystr.split("&")[1].substring(6).split("-");

    var retquery = constractretquery(param,querystr.split("&")[0].split("=")[1],querystr.split("&")[0].split("=")[1]);
    var redirecturl = constractreturl(param,retpath,retquery);
    response.redirect(redirecturl);


    //testcode
    //response.send("snauthcontrol OK  "+redirecturl);
}

//构造返回给ap的参数,函数的参数包括未带参数名的原始请求参数值，sntype for thdtype,thduid
constractretquery=function(paramvalue,thdtype,thduid)
{
    //对应关系
    //param[0]=null                            //null
    //param[1]=guwangtest121                   //ssid
    //param[2]=192.168.1.4                     //wlanuserip
    //param[3]=A0%3A88%3AB4%3A8E%3AD9%3A18     //wlanusermac
    //param[4]=192.168.1.1%3A3834              //gwip
    //param[5]=7f1fbb08                        //token
    //param[6]=00%3A19%3AE0%3A00%3A00%3A11     //apmac
    //param[7]=ZTE00000                        //apsn
    //param[8]=http%3A%2F%2Fwww.baidu.com%2F   //wlanuserfirsturl
    //param[9]=121.199.39.232                  //adwifiip
    //param[10]=80                             //adwifiport

    var retflag = 0;
    var sessionid = '1aorfil9d1nwk1n8p17k5miqdo';
    var custcode = 1128;

    //weibo return parm
    var paramname=['null','ssid','wlanuserip','wlanusermac','gwip','token','apmac','apsn','wlanuserfirsturl','adwifiip','adwifiport'];
    var retquery ='';
    for (i=1;i<=10;i++)
    {

        retquery = retquery + paramname[i]+'='+paramvalue[i]+'&';
        /*
        if(i==10)
        {
            retquery = retquery + paramname[i]+'='+paramvalue[i];
        }
        else
        {
            retquery = retquery + paramname[i]+'='+paramvalue[i]+'&';
        }
        */
    }

    //Add retflag,sessionid,custcode,thdtype,thduid
    retquery = retquery + 'retflag='+retflag +'&'+'sessionid='+sessionid +'&' + 'custcode='+custcode+'&'+'thdtype='+thdtype+'&'+'thduid='+thduid;

    console.log("[SnAuthControl.Servlet]:constract return to ap URL of Query String  = " + retquery);
    return retquery;
}

constractreturl = function(param,path,querystr)
{
    var returl = 'http://'+param[4]+path+querystr;
    console.log("[SnAuthControl.Servlet]:constract return to ap URL = " + returl);
    return returl;

}